﻿$(function () {
    $("#scenic-grid").datagrid({
        url: $.Api("Tour/Scenic/Grid"),
        nowrap: false, rownumbers: true, border: false,
        fit: true, singleSelect: true,
        pageSize: 20, pagination: true,
        idField: "ID", toolbar: "#scenic-tools",
        sortName: "UpdateTime", sortOrder: "desc",
        columns:
        [[
            { field: "Name", title: "景点名称", width: 200, align: "center", sortable: true },
            {
                field: "Img", title: "封面图片", width: 120, align: "center",
                formatter: function (value, row, index) {
                    return $.GetIconButton("icon-images", "查看封面",
                        "Scenic.ShowImg(\"" + value + "\",\"" + row.Name + "\")");
                }
            },
            {
                field: "Virtual", title: "虚拟旅游", width: 120, align: "center",
                formatter: function (value, row, index) {
                    return $.GetIconButton("icon-images_night_scene", "虚拟旅游",
                        "Scenic.ShowVirtual(\"" + value + "\",\"" + row.Name + "\")");
                }
            },
            {
                field: "Voice", title: "语音导航", width: 220, align: "center",
                formatter: function (value, row, index) {
                    return $.GetDewplayer("../userfiles/scenic/voice/" + value, true);
                }
            },
            { field: "InsertTime", title: "添加时间", width: 140, align: "center", sortable: true },
            { field: "UpdateTime", title: "更新时间", width: 140, align: "center", sortable: true },
            {
                field: "Top", title: "顶置", width: 50, align: "center", sortable: true,
                formatter: function (value, row, index) { return $.GetYesOrNo(value); }
            }
        ]]
    });

    $("#scenic-searchbox").searchbox({
        width: 150, prompt: "请输入景点名称查询",
        searcher: function (value) {
            $("#scenic-grid").datagrid("clearChecked");
            $("#scenic-grid").datagrid("load", { search: value });
        }
    });
});

var Scenic = {
    //添加
    Insert: function () {
        Scenic.Mutual("添加", "tour/scenic/Edit.aspx");
    },
    //修改
    Update: function () {
        var row = $("#scenic-grid").datagrid("getSelected");
        if (row) {
            Scenic.Mutual("修改", "tour/scenic/Edit.aspx?id=" + row.ID);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },
    //删除
    Delete: function () {
        var row = $("#scenic-grid").datagrid("getSelected");
        if (row) {
            $.EasyuiConfirm("确认删除?", function (del) {
                if (del) {
                    $.AjaxMask({
                        url: $.Api("Tour/Scenic/Delete"),
                        type: "post", data: { id: row.ID },
                        success: function (result) {
                            result = JSON.parse(result);
                            $.EasyuiSlide(result.Message);
                            if (result.Success) { Scenic.Reload(); }
                        }
                    });
                }
            });
        }
        else { $.EasyuiSlide(delnullrowmsg); }
    },
    //实体操作
    Mutual: function (title, href) {
        $.ModelDialog({
            title: title, href: href, width: 668,
            handler: function () {
                $.ModelForm({
                    validate: true, button: $(this),
                    editor: "#scenic-content",
                    id: "#scenic-model",
                    url: $.Api("Tour/Scenic/Save"),
                    success: function (result) { Scenic.Reload(); }
                });
            },
            onLoad: function () { Scenic.FormInit(); }
        });
    },
    //弹窗初始化
    FormInit: function () {
        $.EditorInit("#scenic-content", 200, "p", true);

        var mp3file = $("#scenic-voice").val();
        $("#scenic-player").html($.GetDewplayer("../userfiles/scenic/voice/" + mp3file, false));
    },

    //预览
    Preview: function () {
        var row = $("#scenic-grid").datagrid("getSelected");
        if (row) {
            window.open("../Scenic.aspx?id=" + row.ID);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //顶置
    Top: function () {
        var row = $("#scenic-grid").datagrid("getSelected");
        if (row) {
            $.AjaxMask({
                url: $.Api("Tour/Scenic/Top"),
                type: "post", data: { id: row.ID },
                success: function (result) {
                    result = JSON.parse(result);
                    $.EasyuiSlide(result.Message);
                    if (result.Success) { Scenic.Reload(); }
                }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //刷新列表
    Reload: function () {
        $.DataGridReload("#scenic-grid");
    },

    //显示封面
    ShowImg: function (file, text) {
        if (!file) { text = "查看封面"; file = $("#scenic-img").val(); }

        var img = "<img style='display:block; margin:auto;' src='../userfiles/scenic/big/" + file + "' />";
        var box = "<div style='width:" + 800 + "px;height:" + 480 + "px;'>" + img + "</div>";

        $.ModelDialog({ title: text, width: 814, iconCls: "icon-images", content: box });
    },
    //上传封面
    AddImg: function () {
        KindEditor.editor({
            uploadJson: $.Api("Tour/Scenic/AddImg"), formatUploadUrl: false
        }).loadPlugin("image", function () {
            this.plugin.imageDialog({
                showRemote: false,
                clickFn: function (url, title, width, height, border, align) {
                    $.EasyuiSlide("上传成功!"); this.hideDialog(); $("#scenic-img").val(url);
                }
            });
        });
    },
    //清除封面
    DeleteImg: function () {
        $("#scenic-img").val("null.png"); $.EasyuiSlide("清除成功!");
    },

    //显示虚拟
    ShowVirtual: function (file, text) {
        if (!file) { text = "查看虚拟"; file = $("#scenic-virtual").val(); }

        var box = $("<div />").html('\
            <object type="application/x-shockwave-flash" id="krpanoSWFObject" width="800" height="480" '+
               'name="krpanoSWFObject" bgcolor="#000000"' +
               'data="../public/pano/krpano.swf?pano=../public/pano/krpano.xml&sphere=' +
               '../userfiles/scenic/virtual/' + file + '">' +

               '<param name="allowScriptAccess" value="sameDomain" />' +
               '<param name="allowFullScreen" value="true" />' +
               '<param name="allowscriptaccess" value="always" />' +
               '<param name="quality" value="high" />' +
               '<param name="movie" value="../public/pano/krpano.swf?pano=../public/pano/krpano.xml&sphere=' +
               '../userfiles/scenic/virtual/' + file + '"/>' +

               '<!--[if IE>-->' +
               '<embed src="../public/pano/krpano.swf?pano=../public/pano/krpano.xml&sphere=' +
               '../userfiles/scenic/virtual/' + file +
               '" style="" id="krpanoSWFObject" ' +
               'type="application/x-shockwave-flash"' +
               'name="krpanoSWFObject" bgcolor="#000000" quality="high" ' +
               'allowfullscreen="true" allowscriptaccess="always" width="800" height="480" ' +
               'pluginspage="http://www.macromedia.com/go/getflashplayer" />' +
               '<!--<![endif]-->' +
           '</object>');

        $.ModelDialog({ title: text, width: 814, height: 556, iconCls: "icon-images_night_scene", content: box });
    },
    //上传虚拟
    AddVirtual: function () {
        KindEditor.editor({
            uploadJson: $.Api("Tour/Scenic/AddVirtual"), formatUploadUrl: false
        }).loadPlugin("image", function () {
            this.plugin.imageDialog({
                showRemote: false,
                clickFn: function (url, title, width, height, border, align) {
                    $.EasyuiSlide("上传成功!"); this.hideDialog(); $("#scenic-virtual").val(url);
                }
            });
        });
    },
    //清除虚拟
    DeleteVirtual: function () {
        $("#scenic-virtual").val("null.png"); $.EasyuiSlide("清除成功!");
    },

    //上传语音
    AddVoice: function () {
        KindEditor.editor({
            uploadJson: $.Api("Tour/Scenic/AddVoice"),
            formatUploadUrl: false
        }).loadPlugin("insertfile", function () {
            this.plugin.fileDialog({
                showRemote: false,
                clickFn: function (url, title) {
                    $.EasyuiSlide("上传成功!"); this.hideDialog(); $("#scenic-voice").val(url);
                    $("#scenic-player").html($.GetDewplayer("../userfiles/scenic/voice/" + url, false));
                }
            });
        });
    }
};